﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SN.Data;
using System.Data;
using System.Text;
using Agro.Models;
using System.Reflection;
using System.Diagnostics;
using Agro.Services;
using Agro.Library;
using System.Drawing;
using Ninject;
using Newtonsoft.Json;
using SN.Core;
using System.Web.Script.Serialization;

namespace Agro.Web.Areas.ActionManager.Controllers
{
    public class ActionController : BaseController
    {
        [Inject]
        public IActionService ActionreService { get; set; }
        //
        // GET: /ActionManager/Action/
        public ActionResult Action_List()
        {
            return View();
        }
        public ActionResult Action_Edit(int? id, string MenuId)
        {
            T_Action Action = new T_Action();
            if (id == null)
            {
                Action.MenuID = MenuId;
                ViewBag.type = "add";//用来前台页面控制“功能编号”可否修改，add时可修改，edit时，不可修改
            }
            else
            {
                Action = ActionreService.GetEntity<T_Action>(T_Action._.ActionID == id);
                ViewBag.type = "edit";
            }
            return View(Action);
        }

        public ActionResult Action_GetList(int? menuid, QueryBase query)
        {
            WhereClip where = WhereClip.All;
            if (menuid != null)
                where = WhereClip.Bracket(T_Action._.MenuID == menuid || T_Menu._.Menu_ParentID==menuid);
           
            var list = ActionreService.GetActionList(query, where);
            return JqGrid(list, query);
        }

        [HttpPost]
        public ActionResult Action_Save(string ActionID, FormCollection collection)
        {
            T_Action Action = new T_Action();
            if (Convert.ToInt32(ActionID) > 0)
            {
                Action = ActionreService.GetEntity<T_Action>(T_Action._.ActionID == Convert.ToInt32(ActionID));
            }
            UpdateModel(Action, collection);
            string msg = string.Empty;
            ActionreService.SaveAction(Action, out msg);
            return SNJson(Action.ActionID, msg);
        }

        [HttpPost]
        public ActionResult DeleteAction(string ids)
        {
            string msg = string.Empty;
            ActionreService.DeleteAction(ids, out msg);
            return SNJson(msg);
        }
    }
}
